<template>
  <div class="houseShareList-container">
    <div class='tab-container'>
      <van-tabs
        v-model="activeIndex"
        color="#007AE6"
        :line-width="20"
        title-active-color="#007AE6"
        sticky
        animated
        @click="onChange"
      >
        <van-tab
          v-for="item in tabs"
          :key="item.index"
          :title="item.label"
        ></van-tab>
      </van-tabs>
    </div>
    <div class="tab-box">
      <Tips v-if="list.length"
      :isBottom="false"
      :isTool="false" 
      title="楼盘分享"
      unit="次"
      :increase="todayCount" 
      :reduce="yesterdayCount" />
      <van-list
        v-model="loading"
        :finished="finished"
        :finished-text="finished && !list.length ? '' : '没有更多了'"
        @load="onLoad"
      >
        <div v-for="(item,index) in list" :key="index">
          <house-share-list
            :item="item"
            @on-click="jump"
          ></house-share-list>
          <NewestDivider v-if="findIndex == index" title="新增楼盘分享" />
        </div>
      </van-list>
    </div>
    <div class="no-data" v-if='!this.list.length && finished'>
      <img src="@/assets/img/custom/no-house-list.png"alt="">
      <p>暂无楼盘分享数据，快去分享吧~ </p>
    </div>
  </div>
</template>
<script>
import houseShareList from 'COMP/Dynamics/houseShareList'
import dynamicsService from 'SERVICE/dynamicsService'
import NewestDivider from '@/components/Dynamics/NewestDivider'
import Tips from '@/components/Dynamics/Tips'
import { Circle } from 'vant';
export default {
  components: {
    Tips,
    houseShareList,
    NewestDivider
  },
  data () {
    return {
      list: [],
      current: 1,
      size: 10,
      loading: false,
      finished: false,
      scrollTop: 0,
      findIndex : 0,
      todayCount: 0,
      activeIndex: 1,
      selectIndex: 1,
      yesterdayCount: 0,
      tabs: [
        { index: 0, label: '文章分享', activeIndex: 3, href: '/dynamics/articleShareList' },
        { index: 1, label: '楼盘分享', activeIndex: 1, href: '/dynamics/houseShareList' },
        { index: 2, label: '名片分享', activeIndex: 2, href: '/dynamics/cardShareList' }
      ],
    }
  },
  created () {
    this.getPosition()
  },
  mounted() {
    this.$nextTick(() =>{
      this.$el.querySelector(".tab-box").scrollTop = this.scrollTop
    })
  },
  methods: {
    /**
     * 上拉加载
     */
    async onLoad () {
      try {
        let result = await dynamicsService.getHouseDynamicList(this.current, this.size)
        if (this.current === 1) {
          this.list = result.records
        } else {
          this.list = [...this.list, ...result.records]
        }
        this.findIndex = this.list.findIndex(item => !item.ifToday) - 1
        if (this.current >= result.pages) {
          this.finished = true
        } else {
          this.current++
        }
        this.loading = false
      } catch (error) {
        console.log(error)
      }
    },
    onChange (index) {
      let current = this.tabs.find(item => item.index === index)
      this.selectIndex = this.tabs[index].activeIndex
      this.$router.replace(current.href)
    },
    /**
     * 获取动态数量
     */
    async getDynamicShareCountVo () {
      try {
        let params = await dynamicsService.getDynamicShareCountVo({ sourceType: 2 })
        this.todayCount = params.todayCount || 0
        this.yesterdayCount = params.yesterdayCount || 0
      } catch (error) {
        console.log(error)
      }
    },
    jump(item) {
      if (Number(item.dynamicCount)) {
        this.$router.push({ name: 'activityTrack', query: { activeIndex: this.selectIndex, shareUuid: item.shareUuid ,count: item.dynamicCount} })
      }
    },
    /**
     * 获取保存位置
     */
    getPosition () {
      try {
        let data = sessionStorage.getItem('shareHouseList')
        if (data) {
          let obj = JSON.parse(data)
          for (const key in obj) {
            this[key] = obj[key]
          }
          sessionStorage.removeItem('shareHouseList')
        } else {
          this.getDynamicShareCountVo()
        }
      } catch (error) {
        console.log(error)
      }
    }
  },
  beforeRouteLeave (to, from, next) {
    if (to.path === '/dynamics/activityTrack') {
      to.meta.title = '楼盘分享数据'
      this.scrollTop = this.$el.querySelector(".tab-box").scrollTop
      sessionStorage.setItem('shareHouseList', JSON.stringify(this.$data))
    }
    next()
  }
}
</script>
<style lang="less" scoped>
.houseShareList-container {
  height: 100%;
  background: rgba(247, 249, 250, 1);
  position: relative;
  .tab-box {
    overflow: scroll;
    height: calc(100% - 44px);
  }
  .no-data {
    width: 100%;
    position: absolute;
    top: 40%;
    left: 50%;
    transform: translate(-50%,-40%);
    font-size: 12px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
    text-align: center;
    img {
      width: 88px;
      height: 88px;
    }
  }
}
</style>
